package YAPclient.sound;

import java.io.BufferedInputStream;
import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
//import javax.sound.sampled.FloatControl;

/**
 * 
 */
import YAPclient.DataModul;
import YAPenvironment.YAPconstant;

public class Sound implements ISound{
	URL soundURL;
	Clip[] clip = new Clip[4];
	public Sound(URL soundURL, int numberOfClips){
		this.soundURL = soundURL;
		clip = new Clip[numberOfClips];
		try{
			AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundURL);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(audioInputStream);
			AudioFormat af = audioInputStream.getFormat();
			int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
			byte[] audio = new byte[size];
			bufferedInputStream.read(audio, 0, size);
		
			//comment this in to test which mixer's are available
//			for(Mixer.Info mixerInfo: AudioSystem.getMixerInfo()){
//				try{
//					Clip testClip = AudioSystem.getClip(mixerInfo);
//					System.out.println("This mixer works: "+mixerInfo.getName());
//					System.out.println(mixerInfo.getDescription());
//				}catch(Exception e){
//					e.printStackTrace();
//					System.out.println("Mixer does not work: "+mixerInfo.getName());
//				}finally{
//					System.out.println();
//				}
//			}
			
			//create new clips to be played
			for(int i=0; i<clip.length; i++){
				clip[i] =  AudioSystem.getClip(AudioSystem.getMixerInfo()[0]);
				try{
					clip[i].open(af, audio, 0, size);
				}catch(IllegalArgumentException e){
					clip[i] =  AudioSystem.getClip(AudioSystem.getMixerInfo()[1]);
					clip[i].open(af, audio, 0, size);
				}

//				FloatControl gainControl = 
//	    		    (FloatControl) clip[i].getControl(FloatControl.Type.MASTER_GAIN);
//				if(this.equals(YAPconstant.Sound.bounce))
//					gainControl.setValue(-30.0f);
//				else
//					gainControl.setValue(-12.0f);
			}
			
			
		}catch(Exception e){
			//turn the sound off if exception is caught
//			DataModul.clPlayer.setSound(false);
			if(YAPconstant.C_DEBUG){
				e.printStackTrace(); 
				}
		}
	}
	public void play(){
		if(soundURL==null /*|| !DataModul.clPlayer.getSound()*/)
			return;
		//if == false then the sound is turned off and one shouldn't here any clips playing...
		if(DataModul.clPlayer.getSound()){
			try{			
				//maximimum number of clips of same waveform running. If all are in use then the first one is liberated and restarted.
				for(int i=0; i<clip.length; i++){
					if(clip[i].isActive() == false){
						clip[i].stop();
						clip[i].setFramePosition(0);
						clip[i].start();
						return;
					}else if(i == clip.length-1){
						clip[0].stop();
						clip[0].setFramePosition(0);
						clip[0].start();
						return;
					}
				}
	        }catch(Exception e){
//	        	DataModul.clPlayer.setSound(false);
	        	if(YAPconstant.C_DEBUG){ 
	        		e.printStackTrace();
	        		}
	        }
		}
	}
	/**
	 * Returns <code>true</code> if the clip at index i of the array of clips is playing.
	 * @param i index of the clip in the array
	 * @return <code>true</code> if the clip at index i of the array of clips is playing
	 */
	public boolean isPlaying(int i){
		return clip[i].isActive();
	}
	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}
}